﻿@using ArithmeticGenerator.Models
@using ArithmeticGenerator.QuestionBuilder

<style>
    .my-custom-class {
        backdrop-filter: blur(10px);
    }

    .custom-striped {
        background: repeating-linear-gradient( 45deg, #009688, #009688 10px, #00675B 10px, #00675B 20px );
        color: white !important;
    }
</style>

<NumberSelector NumberChanged="@OnNumber1Changed"></NumberSelector>
<MudToggleGroup @bind-Value="Operator"
                T="OperatorEnum"
                Class="my-2"
                Size="Size.Small"
                SelectedClass="@ToggleStyle"
                Style="width: 10rem">
    <MudToggleItem Value="@(OperatorEnum.Add)" Text="@(OperatorEnum.Add.GetDescription())" />
    <MudToggleItem Value="@(OperatorEnum.Subtract)" Text="@(OperatorEnum.Subtract.GetDescription())" />
    <MudToggleItem Value="@(OperatorEnum.Multiply)" Text="@(OperatorEnum.Multiply.GetDescription())" />
    <MudToggleItem Value="@(OperatorEnum.Divide)" Text="@(OperatorEnum.Divide.GetDescription())" />
</MudToggleGroup>

<NumberSelector NumberChanged="@OnNumber2Changed"></NumberSelector>

<MudPaper Class="mt-2" Elevation="0">
    <ResultLengthSelector @bind-MinLength="@(_questionRule.MinLength)"
                          @bind-MaxLength="@(_questionRule.MaxLength)"
                          LengthChanged="CreateNumberExpression" />
</MudPaper>

<MudPaper Elevation="0" Class="mt-2" Height="30px">
    <MudChipSet @bind-SelectedValues="ResultRules"
                T="ResultRuleEnum"
                SelectionMode="SelectionMode.MultiSelection"
                CheckMark="true"
                Variant="Variant.Filled"
                Color="Color.Primary">

        <MudChip Value="@(ResultRuleEnum.ResultUseUnderline)" Text="@(ResultRuleEnum.ResultUseUnderline.GetDescription())" />

        @if (Operator == OperatorEnum.Subtract)
        {
            <MudChip Value="@(ResultRuleEnum.GreaterThanZero)" Text="@(ResultRuleEnum.GreaterThanZero.GetDescription())" />
        }
        @if (Operator == OperatorEnum.Multiply)
        {
            <MudChip Value="@(ResultRuleEnum.ValueIsNotOne)" Text="@(ResultRuleEnum.ValueIsNotOne.GetDescription())" />
        }
        @if (Operator == OperatorEnum.Divide)
        {
            <MudChip Value="@(ResultRuleEnum.IsInt)" Text="@(ResultRuleEnum.IsInt.GetDescription())" />
            <MudChip Value="@(ResultRuleEnum.ValueIsNotOne)" Text="@(ResultRuleEnum.ValueIsNotOne.GetDescription())" />
            <MudChip Value="@(ResultRuleEnum.ResultIsNotOne)" Text="@(ResultRuleEnum.ResultIsNotOne.GetDescription())" />
        }
    </MudChipSet>
</MudPaper>
<MudPaper Class="d-flex align-center mt-4" Elevation="0">
    <MudButton Variant="Variant.Filled"
               StartIcon="@Icons.Material.Filled.Save"
               Color="Color.Primary"
               Size="Size.Small"
               Disabled="@(_sampleQuestion.IsEmpty())"
               OnClick="AddNumberExpression">添加题型</MudButton>
    <MudText Typo="Typo.subtitle2" Class="ml-3">
        @if (_sampleQuestion.IsEmpty())
        {
            @($"示例：不存在这样的等式！")
        }
        else
        {
            @($"示例：{_sampleQuestion}")
        }

    </MudText>
</MudPaper>

@code {
    private const string ToggleStyle = "custom-striped";

    private OperatorEnum _operator2;
    public OperatorEnum Operator
    {
        get => _operator2;
        set
        {
            _operator2 = value;
            CreateNumberExpression();
        }
    }
    private QuestionRule _questionRule = new QuestionRule() { MinLength = 0, MaxLength = 99999 };
    private IReadOnlyCollection<ResultRuleEnum> _resultRules = null!;
    public IReadOnlyCollection<ResultRuleEnum> ResultRules
    {
        get => _resultRules;
        set
        {
            _resultRules = value;
            _questionRule.ResultRule = CombineRules(_resultRules);
            CreateNumberExpression();
        }
    }

    private CustomNumber? _number1;
    private CustomNumber? _number2;

    private string _sampleQuestion = "";


    [Parameter]
    public EventCallback<QuestionExpression> ExpressionSelected { get; set; }

    [Inject]
    private QuestionFactory QuestionFactory { get; set; } = default!;

    protected override void OnInitialized()
    {
        Operator = OperatorEnum.Add;
        base.OnInitialized();
    }

    private void OnNumber1Changed(CustomNumber number)
    {
        _number1 = number;
        CreateNumberExpression();
    }
    private void OnNumber2Changed(CustomNumber number)
    {
        _number2 = number;
        CreateNumberExpression();
    }

    private void CreateNumberExpression()
    {
        _sampleQuestion = "";
        if (_number1 != null && _number2 != null)
        {
            _sampleQuestion = QuestionFactory.Create(Operator, _number1, _number2).GenerateQuestion(_questionRule);
        }
    }

    public static ResultRuleEnum CombineRules(IReadOnlyCollection<ResultRuleEnum> rules)
    {
        ResultRuleEnum combinedRule = 0;
        foreach (var rule in rules)
        {
            combinedRule |= rule;
        }
        return combinedRule;
    }

    private async Task AddNumberExpression()
    {
        if (_number1 == null || _number2 == null)
        {
            return;
        }
        var questionRule = new QuestionRule()
            {
                MaxLength = _questionRule.MaxLength,
                MinLength = _questionRule.MinLength,
                ResultRule = _questionRule.ResultRule
            };
        await ExpressionSelected.InvokeAsync(new QuestionExpression(_number1, Operator, _number2, questionRule));
    }
}
